import {
  LOGIN_START,
  LOGIN_SUCCESS,
  LOGIN_FAIL,
  LOGOUT_SUCCESS
} from '../actions/actionType'
//读取本地是否有用户登录信息
const usermessage = JSON.parse(window.sessionStorage.getItem('usermessage'))
// const userData = Object.assign({ displayName: '' }, usermessage)
const initStates = {
  ...usermessage,
  isloading: false
  //   haslogin: usermessage ? true : false
}
export default (state = initStates, action) => {
  switch (action.type) {
    case LOGIN_START:
      //   console.log('登录开始')
      return {
        ...state,
        isloading: true
      }
    case LOGIN_SUCCESS:
      //   console.log('登录成功')
      //登录成功将用户信息保存
      window.sessionStorage.setItem(
        'usermessage',
        JSON.stringify(action.payload.data)
      )
      console.log(action.payload.data)
      return {
        ...state,
        ...action.payload.data,
        // haslogin: true,
        isloading: false
      }
    case LOGOUT_SUCCESS:
      //注销成功,删除本地用户信息
      //   window.localStorage.removeItem('usermessage')
      window.sessionStorage.removeItem('usermessage')
      return {
        ...state,
        ...action.payload.data
        // haslogin: false
      }
    case LOGIN_FAIL:
      //   console.log('登录失败')
      return {
        ...state,
        isloading: false,
        ...action.payload.data
      }

    default:
      return state
  }
}
